package cn.bankAutoOffice.service.impl;

import cn.bankAutoOffice.bean.entity.Department;
import cn.bankAutoOffice.bean.entity.Employee;
import cn.bankAutoOffice.bean.entity.Permission;
import cn.bankAutoOffice.mapper.EmployeeMapper;
import cn.bankAutoOffice.service.EmployeeService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 金健伟
 * @DATE 2023/4/11 10:58
 * 功能说明：
 */

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 通过员工名称查询单条数据
     *
     * @param empCode 主键
     * @return 实例对象
     */
    @Override
    public Employee queryByName(Integer empCode) {
        return employeeMapper.queryByName(empCode);
    }

    /**
     * 新增数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insert(Employee employee) {
        String password = employee.getEmpPassword();
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        int times = 2;
        String encodedPwd = new SimpleHash("md5", password, salt, times).toString();
        employee.setEmpPassword(encodedPwd);
        employee.setSalt(salt);
        return employeeMapper.insert(employee);
    }

    /**
     * 通过部门id获取部门名称
     *
     * @param departmentId 部门id
     * @return 部门
     */
    public List<Department> getDepartment(Integer departmentId){
        return employeeMapper.getDepartment(departmentId);
    }

    /**
     * 通过部门id获取权限
     *
     * @param departmentId 部门id
     * @return 部门
     */
    public List<Permission> getPermission(@Param("departmentId") Integer departmentId){
        return employeeMapper.getPermission(departmentId);
    }

}
